package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.vo.PageVo;

import com.hmall.item.constant.MqConstant;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.common.pojo.Item;
import com.hmall.item.service.IItemService;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageVo pageQuery(Long page ,Long size) {
        Page pages = new Page(page,size);
        LambdaQueryWrapper<Item> itemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page pageResult = itemMapper.selectPage(pages, itemLambdaQueryWrapper);
        return new PageVo(pageResult.getPages(),pageResult.getRecords());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Item findItemById(Long id) {
        Item item = itemMapper.selectById(id);
        return item;
    }

    @Override
    public void updateEs(Item item) {
        if (item.getStatus() == 1){
            rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_ON_SALE_KEY,item.getId());
        }else {
            rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_OUT_SALE_KEY,item.getId());
        }
    }

}
